package com.duoma.view.ui.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.AnimationSet;
import android.view.animation.AnimationUtils;
import android.view.animation.RotateAnimation;
import android.view.animation.ScaleAnimation;
import android.view.animation.TranslateAnimation;

import com.duoma.view.R;

public class AnimTweenActivity extends AppCompatActivity implements View.OnClickListener{

    private View btn_1;
    private Animation animAlpha;
    private Animation animScale;
    private Animation animTranslate;
    private Animation animRotate;

    private Animation animSet;

    private Animation animAlphaCode;
    private Animation animScaleCode;
    private Animation animTranslateCode;
    private Animation animRotateCode;

    private AnimationSet animSetCode;

    private Animation animTranslateCode1;

    public static void start(Context context) {
        Intent intent = new Intent(context, AnimTweenActivity.class);
        context.startActivity(intent);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_anim);

        initUi();
        initAnim();
    }

    private void initAnim() {
        /**
         * 代码创建图片渐变(Alpha)动画
         * 实例化AlphaAnimation对象：
         * mAnimation = new AlphaAnimation(fromAlpha, toAlpha);
         * fromAlpha:设置动画开始时控件的透明度，0.0为透明，控件不显示，1.0为不透明，控件全部显示
         * toAlpha:设置动画结束时的控件的透明度
         */
        animAlphaCode = new AlphaAnimation(0.0f, 1.0f);
        animAlphaCode.setDuration(3000);

        /**
         * 代码创建图片渐变(Scale)动画
         * mAnimation = new ScaleAnimation(fromX, toX, fromY, toY, pivotXType, pivotXValue, pivotYType, pivotYValue);
         * fromX:设置动画开始时X轴方向的缩放起始值。0.0为X轴方向的控件缩成一点，1.0为X轴方向的控件不缩放;
         * toX:设置动画结束时X轴方向的缩放结束值;
         * fromY:设置动画开始时Y轴方向的缩放起始值。0.0为Y轴方向的控件缩成一点，1.0为Y轴方向的控件不缩放;
         * toY:设置动画结束时Y轴方向的缩放结束值;
         * pivotXtype:动画在X轴相对于物件位置类型 ,分为RELATIVE_TO_SELF、RELATIVE_TO_PARENT和ABSOLUTE三种类型：
         *                       1、RELATIVE_TO_SELF：相对于控件自身；
         *                       2、RELATIVE_TO_PARENT：相对于父控件；
         *                       3、ABSOLUTE：绝对坐标；
         * pivotXValue:动画相对于物件的X坐标的开始位置;
         * pivotYType:动画在Y轴相对于物件位置类型 ,分为RELATIVE_TO_SELF、RELATIVE_TO_PARENT和ABSOLUTE三种类型;
         * pivotYValue:动画相对于物件的Y坐标的开始位置;
         */
        //mAnimation = new ScaleAnimation(fromX, toX, fromY, toY)
        //mAnimation = new ScaleAnimation(fromX, toX, fromY, toY, pivotX, pivotY)
        //mAnimation = new ScaleAnimation(fromX, toX, fromY, toY, pivotXType, pivotXValue, pivotYType, pivotYValue)
        animScaleCode = new ScaleAnimation(0.0f, 1.0f, 0.5f, 1.0f, Animation.RELATIVE_TO_SELF, 1.0f, Animation.RELATIVE_TO_SELF, 1.0f);
        animScaleCode.setDuration(3000);

        /**
         * 代码创建图片位置位移(Translate)动画
         * mAnimation = new TranslateAnimation(fromXType, fromXValue, toXType, toXValue, fromYType,
         *  fromYValue, toYType, toYValue);
         * fromXType:动画开始执行时在X轴相对于物件位置类型
         * fromXValue:动画开始执行时X轴方向的的起始位置，当位置类型为RELATIVE_TO_SELF时，Value取0.0f~1.0f之间，
         * 当位置类型为RELATIVE_TO_PARENT或ABSOLUTE时， Value使用(px)像素值
         * toXType:动画结束执行时在X轴相对于物件位置类型
         * toXValue:动画结束执行时X轴方向的的结束位置，Value取值方式同上
         * fromYType:动画开始执行时在Y轴相对于物件位置类型
         * fromYValue:动画开始执行时Y轴方向的的起始位置，Value取值方式同上
         * toYType:动画在结束执行时在Y轴相对于物件位置类型
         * toYValue:动画结束执行时Y轴方向的的结束位置，Value取值方式同上
         */
        //mAnimation = new TranslateAnimation(fromXDelta, toXDelta, fromYDelta, toYDelta);
        animTranslateCode = new TranslateAnimation(Animation.RELATIVE_TO_SELF, 0.0f, Animation.RELATIVE_TO_SELF, 1.0f,
                Animation.RELATIVE_TO_SELF, 0.0f, Animation.RELATIVE_TO_SELF, 1.0f);
        animTranslateCode.setDuration(3000);

        /**
         * 代码创建图片旋转(Rotate)动画
         * mAnimation = new RotateAnimation(fromDegrees, toDegrees, pivotXType, pivotXValue, pivotYType, pivotYValue)；
         * fromDegrees：动画开始执行时的控件起始状态的角度;
         * toDegrees：动画结束执行时的控件结束状态的角度；
         * pivotXType：动画在X轴相对于物件位置类型 ,分为RELATIVE_TO_SELF、RELATIVE_TO_PARENT和ABSOLUTE三种类型：
         *                       1、RELATIVE_TO_SELF：相对于控件自身；
         *                       2、RELATIVE_TO_PARENT：相对于父控件；
         *                       3、ABSOLUTE：绝对坐标；
         * pivotXValue：动画开始执行时X轴方向的的起始位置，当位置类型为RELATIVE_TO_SELF时，Value取0.0f~1.0f之间，当位置类型为RELATIVE_TO_PARENT或ABSOLUTE时，
         *              Value使用(px)像素值;
         * pivotYType：动画在Y轴相对于物件位置类型 ,分为RELATIVE_TO_SELF、RELATIVE_TO_PARENT和ABSOLUTE三种类型;
         * pivotYValue：旋转动画的中心店的Y轴方向的纵坐标，原理同上；
         */
        //mAnimation = new RotateAnimation(fromDegrees, toDegrees)
        //mAnimation = new RotateAnimation(fromDegrees, toDegrees, pivotX, pivotY)
        animRotateCode = new RotateAnimation(-50f, 360f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
        animRotateCode.setDuration(3000);

        /**
         * 代码创建图片集合(Set)动画
         */
        animSetCode = new AnimationSet(true);
        //定义渐变动画对象
        AlphaAnimation mAlphaAnimation = new AlphaAnimation(1.0f, 0.0f);
        mAlphaAnimation.setRepeatCount(1);
        mAlphaAnimation.setRepeatMode(Animation.REVERSE);
        mAlphaAnimation.setFillAfter(true);
        mAlphaAnimation.setDuration(1000);
        //定义缩放动画对象
        ScaleAnimation mScaleAnimation = new ScaleAnimation(1.0f, 0.0f, 1.0f, 0.0f,
                Animation.RELATIVE_TO_SELF,0.5f,Animation.RELATIVE_TO_SELF,0.5f);
        mScaleAnimation.setRepeatCount(1);
        mScaleAnimation.setRepeatMode(Animation.REVERSE);
        mScaleAnimation.setFillAfter(true);
        mScaleAnimation.setDuration(1000);
        //定义位移动画对象
        TranslateAnimation mTranslateAnimation = new TranslateAnimation(Animation.RELATIVE_TO_SELF, 0.0f,
                Animation.RELATIVE_TO_SELF, 1.0f, Animation.RELATIVE_TO_SELF, 0.0f, Animation.RELATIVE_TO_SELF, 1.0f);
        mTranslateAnimation.setRepeatCount(1);
        mTranslateAnimation.setRepeatMode(Animation.REVERSE);
        mTranslateAnimation.setFillAfter(true);
        mTranslateAnimation.setDuration(1000);
        //定义旋转动画对象
        RotateAnimation mRotateAnimation = new RotateAnimation(0, 360, Animation.RELATIVE_TO_SELF, 0.5f,
                Animation.RELATIVE_TO_SELF, 0.5f);
        mRotateAnimation.setRepeatCount(1);
        mRotateAnimation.setRepeatMode(Animation.REVERSE);
        mRotateAnimation.setFillAfter(true);
        mRotateAnimation.setDuration(1000);
        //添加动画到集合动画对象中
        animSetCode.addAnimation(mAlphaAnimation);
        animSetCode.addAnimation(mScaleAnimation);
        animSetCode.addAnimation(mTranslateAnimation);
        animSetCode.addAnimation(mRotateAnimation);
    }

    private void initUi() {
        btn_1 = findViewById(R.id.btn_1);

        findViewById(R.id.button).setOnClickListener(this);

        animAlpha = AnimationUtils.loadAnimation(this, R.anim.view_animation_alpha);
        animScale = AnimationUtils.loadAnimation(this, R.anim.view_animation_scale);
        animTranslate = AnimationUtils.loadAnimation(this, R.anim.view_animation_translate);
        animRotate = AnimationUtils.loadAnimation(this, R.anim.view_animation_rotate);
        animSet = AnimationUtils.loadAnimation(this, R.anim.view_animation_set);


        animTranslateCode1 = new TranslateAnimation(Animation.RELATIVE_TO_SELF, 0.0f, Animation.RELATIVE_TO_SELF, 0.0f,
                Animation.RELATIVE_TO_SELF, -1.0f, Animation.RELATIVE_TO_SELF, 0.0f);
        animTranslateCode1.setDuration(3000);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.button:
//                btn_1.startAnimation(animAlpha);
//                btn_1.startAnimation(animScale);
//                btn_1.startAnimation(animTranslate);
//                btn_1.startAnimation(animRotate);

//                btn_1.startAnimation(animSet);

//                btn_1.startAnimation(animAlphaCode);
//                btn_1.startAnimation(animScaleCode);
//                btn_1.startAnimation(animTranslateCode);
//                btn_1.startAnimation(animRotateCode);

//                btn_1.startAnimation(animSetCode);
//                btn_1.startAnimation(animTranslateCode1);

                Animation animSetd = AnimationUtils.loadAnimation(this, R.anim.view_anim_show_down);
                btn_1.startAnimation(animSetd);
                break;
        }
    }
}
